import Joi from 'joi';
import type { Response } from 'express';

// 封装的校验参数工具
const joiValidate = (schema: any, val: any, res?: Response) => {
	const { error, value } = schema.validate(val);
	if (error) {
		if (res) res.json(global.formatResJson({ code: 'Fail', msg: error?.message }));
		throw error?.message;
	}

	return value;
};

global.joi = Joi;
global.joiValidate = joiValidate;
